//MINI UI解析
mini.parse();

/**
 * 收货人信息处理器类
 */
function ConsigneeInfoHandler() {
};

ConsigneeInfoHandler.instance = new ConsigneeInfoHandler();
ConsigneeInfoHandler.mgrData = new OrderMgrData();
ConsigneeInfoHandler.consigneeInfoForm = new mini.Form("consigneeInfoForm");
ConsigneeInfoHandler.boiCountry = mini.get("boiCountry");
ConsigneeInfoHandler.boiProvince = mini.get("boiProvince");
ConsigneeInfoHandler.boiCity = mini.get("boiCity");
ConsigneeInfoHandler.boiDistrict = mini.get("boiDistrict");
ConsigneeInfoHandler.boiId = null;

/**
 * json数据转换
 * @param {} data
 * @return {}
 */
ConsigneeInfoHandler.transformData = function(data){
	var transData = [ConsigneeInfoHandler.mgrData.selectionData[0]];
	if(data != null && data.length > 0){
		for (var i = 0; i < data.length; i++) {
			var item = {
				id : data[i].bdrId,
				text : data[i].bdrName
			};
			transData[i + 1] = item;
		}
	}
	
	return transData
}

/**
 * 加载区域名称处理程序
 * @param {} regionId
 * @param {} bdrParent
 * @param {} level
 */
ConsigneeInfoHandler.loadRegionName = function(bdrId, bdrParent, level){
	$.ajax({
		url : ConsigneeInfoHandler.mgrData.loadRegionInfoListUrl(null, bdrParent),
		cache : false,
		dataType : 'json',
		success : function(text) {
			var tempData = ConsigneeInfoHandler.transformData(text);
				
			if(level == 1){
				ConsigneeInfoHandler.boiCountry.setData(tempData);
				ConsigneeInfoHandler.boiCountry.setValue(bdrId);
			}else if(level == 2){
				ConsigneeInfoHandler.boiProvince.setData(tempData);
				ConsigneeInfoHandler.boiProvince.setValue(bdrId);
			}else if(level == 3){
				ConsigneeInfoHandler.boiCity.setData(tempData);
				ConsigneeInfoHandler.boiCity.setValue(bdrId);
			}else if(level == 4){
				ConsigneeInfoHandler.boiDistrict.setData(tempData);
				ConsigneeInfoHandler.boiDistrict.setValue(bdrId);
			}
			
		},
		error: function(){
			MiniuiTools.miniError("系统提示","数据加载出错！");
		}
	})
}


/**
 * 标准方法接口定义
 * @param {} data
 */
ConsigneeInfoHandler.passData = function(data) {
    data = mini.clone(data);
    ConsigneeInfoHandler.boiId = data.boiId;
	
    $.ajax({
		url : ConsigneeInfoHandler.mgrData.loadOrderInfoUrl(data.boiId),
		cache : false,
		dataType : 'json',
		success : function(text) {
			if(text != null && text != ""){
				ConsigneeInfoHandler.consigneeInfoForm.setData(text);
				
				ConsigneeInfoHandler.loadRegionName(text.boiCountry, "0", 1);
				ConsigneeInfoHandler.loadRegionName(text.boiProvince, text.boiCountry, 2);
				ConsigneeInfoHandler.loadRegionName(text.boiCity, text.boiProvince, 3);
				ConsigneeInfoHandler.loadRegionName(text.boiDistrict, text.boiCity, 4);
			}
		},
		error: function(){
			MiniuiTools.miniError("系统提示","数据加载出错！");
		}
	});
}

/**
 * 加载国家名称
 * @param {} data
 */
ConsigneeInfoHandler.loadCountryData = function(data){
	ConsigneeInfoHandler.boiCountry.setData(null);
	ConsigneeInfoHandler.boiCountry.setData(data);
	ConsigneeInfoHandler.boiCountry.setValue(ConsigneeInfoHandler.mgrData.selectionData[0].id);
}

/**
 * 加载省份名称
 * @param {} data
 */
ConsigneeInfoHandler.loadProvinceData = function(data){
	ConsigneeInfoHandler.boiProvince.setData(null);
	ConsigneeInfoHandler.boiProvince.setData(data);
	ConsigneeInfoHandler.boiProvince.setValue(ConsigneeInfoHandler.mgrData.selectionData[0].id);
}

/**
 * 加载城市名称
 * @param {} data
 */
ConsigneeInfoHandler.loadCityData = function(data){
	ConsigneeInfoHandler.boiCity.setData(null);
	ConsigneeInfoHandler.boiCity.setData(data);
	ConsigneeInfoHandler.boiCity.setValue(ConsigneeInfoHandler.mgrData.selectionData[0].id);
}

/**
 * 加载区县名称
 * @param {} data
 */
ConsigneeInfoHandler.loadDistrictData = function(data){
	ConsigneeInfoHandler.boiDistrict.setData(null);
	ConsigneeInfoHandler.boiDistrict.setData(data);
	ConsigneeInfoHandler.boiDistrict.setValue(ConsigneeInfoHandler.mgrData.selectionData[0].id);
}

/**
 * 加载指定的区域名称
 * @param {} bdrParent
 * @param {} level
 */
ConsigneeInfoHandler.loadRegionData = function(bdrParent, level){
	$.ajax({
		url : ConsigneeInfoHandler.mgrData.loadRegionInfoListUrl(null, bdrParent),
		cache : false,
		dataType : 'json',
		success : function(text) {
			var tempData = ConsigneeInfoHandler.transformData(text);
				
			if(level == 1){
				ConsigneeInfoHandler.loadProvinceData(tempData);
				ConsigneeInfoHandler.loadCityData(ConsigneeInfoHandler.mgrData.selectionData);
				ConsigneeInfoHandler.loadDistrictData(ConsigneeInfoHandler.mgrData.selectionData);
			}else if(level == 2){
				ConsigneeInfoHandler.loadCityData(tempData);
				ConsigneeInfoHandler.loadDistrictData(ConsigneeInfoHandler.mgrData.selectionData);
			}else if(level == 3){
				ConsigneeInfoHandler.loadDistrictData(tempData);
			}
			
		},
		error: function(){
			MiniuiTools.miniError("系统提示","数据加载出错！");
		}
	})
}

/**
 * 区域值改变事件处理程序
 * @param {} level
 */
ConsigneeInfoHandler.selectRegion = function(level){
	if(level == 1){
		ConsigneeInfoHandler.loadRegionData(ConsigneeInfoHandler.boiCountry.getValue(), level);
	}else if(level == 2){
		ConsigneeInfoHandler.loadRegionData(ConsigneeInfoHandler.boiProvince.getValue(), level);
	}else if(level == 3){
		ConsigneeInfoHandler.loadRegionData(ConsigneeInfoHandler.boiCity.getValue(), level);
	}
}

/**
 * 确定按钮点击事件处理程序
 */
ConsigneeInfoHandler.updateConsigneeInfo = function(){
	ConsigneeInfoHandler.consigneeInfoForm.validate();
    if (ConsigneeInfoHandler.consigneeInfoForm.isValid() == false) return;
    
	var formData = ConsigneeInfoHandler.consigneeInfoForm.getData();
	formData.boiId = ConsigneeInfoHandler.boiId;
    var json = mini.encode(formData);
    
    $.ajax({
        url: ConsigneeInfoHandler.mgrData.updateConsigneeInfoUrl(),
        type: "POST",
        data: {
        	orderInfo: json,
        	operateUType: gbOperateUType,
    		operateUser: gbOperateUser,
    		operateIp: gbOperateIp
        },
        cache: false,
        dataType: 'text',
        success: function (count) {
        	if(count != 0){
        		MiniuiTools.miniInfo("系统提示","保存成功！", function(action){
	        		ConsigneeInfoHandler.closeCurrentWin("save");
	        	});
        	}
        },
        error: function (jqXHR, textStatus, errorThrown) {
        	MiniuiTools.miniError("系统提示", jqXHR.responseText, function(){
            	ConsigneeInfoHandler.closeCurrentWin();
            });
        }
    });
}

/**
 * 关闭窗口
 * @param {} action
 */
ConsigneeInfoHandler.closeCurrentWin = function(action) {
    if (window.CloseOwnerWindow) window.CloseOwnerWindow(action);
    else window.close();
}

ConsigneeInfoHandler.closeMiniWin = function(){
	ConsigneeInfoHandler.closeCurrentWin("cancel");
}

function validBoiCountry(e){
	if(e.value == "" || e.value == "-1"){
		e.errorText = "请选择国家名称！";
        e.isValid = false;
	}
}

function validBoiProvince(e){
	if(e.value == "" || e.value == "-1"){
		e.errorText = "请选择省份名称！";
        e.isValid = false;
	}
}

function validBoiCity(e){
	if(e.value == "" || e.value == "-1"){
		e.errorText = "请选择城市名称！";
        e.isValid = false;
	}
}

function validBoiDistrict(e){
	if(e.value == "" || e.value == "-1"){
		e.errorText = "请选择区县名称！";
        e.isValid = false;
	}
}